#include "editslider.h"
#include "ui_editslider.h"

#include <QDebug>

EditSlider::EditSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EditSlider)
{
    ui->setupUi(this);

    connect(ui->slider, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChanged(int)));
    connect(ui->slider, SIGNAL(sliderMoved(int)), this, SLOT(onSliderMoved(int)));
    connect(ui->slider, SIGNAL(sliderPressed()), this, SLOT(onSliderPressed()));
    connect(ui->slider, SIGNAL(sliderReleased()), this, SLOT(onSliderReleased()));

    connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(onSpinBoxValueChanged(int)));
    connect(ui->spinBox, SIGNAL(editingFinished()), this, SLOT(onSpinBoxEditingFinished()));
}

EditSlider::~EditSlider()
{
    delete ui;
}

float EditSlider::value() const{
    return ui->spinBox->value();
}

void EditSlider::setMaximum(int value){
    ui->slider->setMaximum(value);
    ui->spinBox->setMaximum(value);

    ui->maximumLabel->setText(QString::number(value));
}

void EditSlider::setMinimum(int value){
    ui->slider->setMinimum(value);
    ui->spinBox->setMinimum(value);

    ui->minimumLabel->setText(QString::number(value));
}

void EditSlider::setRange(int min, int max){
    setMinimum(min);
    setMaximum(max);
}

void EditSlider::setTracking(bool enable){
    ui->slider->setTracking(enable);
}

void EditSlider::onSliderValueChanged(int value){
    emit valueChanged(value);
}

void EditSlider::onSpinBoxValueChanged(int value){
    ui->slider->setValue(value);
}

void EditSlider::onSliderMoved(int value){
    //Set the value in the spinBox
    ui->spinBox->blockSignals(true);
    ui->spinBox->setValue(value);
    ui->spinBox->blockSignals(false);

    emit sliderMoved(value);
}

void EditSlider::onSliderPressed(){
    emit sliderPressed();
}

void EditSlider::onSliderReleased(){
    emit sliderReleased();
    emit editingFinished(ui->spinBox->value());
}

void EditSlider::setValue(int value){
    //Sending the value to the spinbox will trigger
    //a changing the slider, which will then trigger
    //the slider valueChanged signal
    ui->spinBox->setValue(value);
}

void EditSlider::onSpinBoxEditingFinished(){
    emit editingFinished(ui->spinBox->value());
}
